package cn.shengchao.examstar.ai.infrastructure.persistence.repository;

import cn.hutool.core.date.DateTime;
import cn.shengchao.examstar.ai.domain.repository.IAiChatFreeSunRepository;
import cn.shengchao.examstar.ai.infrastructure.dao.AiChatFreeCountDao;
import cn.shengchao.examstar.ai.infrastructure.dao.AiDoCountDao;
import cn.shengchao.examstar.ai.infrastructure.dao.po.AiChatFreeCount;
import cn.shengchao.examstar.ai.infrastructure.dao.po.AiDoCount;
import cn.shengchao.examstar.ai.shared.enums.AiChatFreeType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class AiChatFreeSunRepository implements IAiChatFreeSunRepository {

    @Autowired
    private AiChatFreeCountDao aiChatFreeSumDao;

    @Autowired
    private AiDoCountDao aiDoCountDao;

    @Override
    public Long getAiChatFreeCount(AiChatFreeType type) {
        AiChatFreeCount aiChatFreeSum = aiChatFreeSumDao.selectOne(Wrappers.lambdaQuery(AiChatFreeCount.class).eq(AiChatFreeCount::getType, type.value));
        return aiChatFreeSum.getAiChatSum();
    }

    @Override
    public Long getUserDoCount(Long userId, DateTime startTime, Date endTime) {
        return aiDoCountDao.selectCount(Wrappers.lambdaQuery(AiDoCount.class).eq(AiDoCount::getUserId, userId)
                .ge(startTime != null, AiDoCount::getCreateTime, startTime)
                .le(endTime != null, AiDoCount::getCreateTime, endTime)
        );
    }

    @Override
    public void doAi(Long userId, Long type) {
        AiDoCount aiDoCount = new AiDoCount();
        aiDoCount.setUserId(userId);
        aiDoCount.setType(type);
        aiDoCount.setCreateTime(new Date());
        aiDoCountDao.insert(aiDoCount);
    }
} 